package com.ycq.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@Api(value = "/",description = "ycq的get方法")
public class MyGetMethod {
    @RequestMapping(value = "/getCookies",method = RequestMethod.GET )
    @ApiOperation(value = "获取到Cookies",httpMethod ="get" )
    //get请求的 返回cookies信息
    public String getCookies(HttpServletResponse response){
        //HttpServerletRequest 装请求信息
        //HttpServerResPonse 装响应信息
        Cookie cookie=new Cookie("login","enen");
        response.addCookie(cookie);
        return "恭喜获得cookies";
    }
    /**
     * 客户端携带cookies
     */
    @RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
    @ApiOperation(value = "需要携带参数才能访问的",httpMethod = "get")
    public String getWithCookies(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        if (Objects.isNull(cookies)){
            return "必须携带cookies访问";
        }
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("login")&&cookie.getValue().equals("enen")){
                return "恭喜访问成功";
            }
        }
        return "必须携带cookies访问!!!";
    }
    /**
     * 携带参数的get请求
     * 第一种实现方式url：key&&key
     */
    @RequestMapping(value = "/getwithparam",method = RequestMethod.GET)
    @ApiOperation(value = "get带入参第一种",httpMethod = "get")
    public Map<String,Integer> getList(@RequestParam Integer start,
                                       @RequestParam Integer end){
        Map<String,Integer> myList=new HashMap<>();
        myList.put("鞋子",800);
        myList.put("衣服",200);
        myList.put("糖",10);
        return myList;
    }
    /**
     * 第二种方法
     * url:ip:10/20
     */
    @RequestMapping(value = "/getwithparam/{start}/{end}")
    @ApiOperation(value = "get带入参第二种",httpMethod = "get")
    public Map muGetList(@PathVariable Integer start,
                         @PathVariable Integer end){
        Map<String,Integer> list=new HashMap<>();
        list.put("空气",100);
        list.put("空气1",10);
        list.put("空气2",1400);

        return list;
    }

}
